<?php

require_once 'PHPUnit/Framework.php';

require dirname(__FILE__).'/../../../KenDon.php';

//require dirname(__FILE__).'/../../../KenDon/classes/KD_QTree.php';
KenDon::load('QTree');

//require dirname(__FILE__).'/../../../KenDon/Adapter/KD_Adapter_DB_PDO.php';
KenDon::load('DB_PDO', 'adapter');

class KD_QTreeTest extends PHPUnit_Framework_TestCase{

  protected $DBWrap ;

  protected $DBInfo ;

  protected $kd_db_pdo ;

  protected $fixture ;

  protected $testData ;

  public function __construct(){
    $this->DBWrap = 'KD_Adapter_DB_PDO';
    $this->DBInfo = array( 'dbms'=>'sqlite2', 'dbName'=> dirname(__FILE__) . '/../../kendon.sdb' );
    $this->kd_db_pdo = new KD_Adapter_DB_PDO($this->DBInfo);  
  }

  public function setUp(){

    //删除数据表
    $sql_drop_table = 'DRUP TABLE IF EXISTS [qtree];';

    $this->kd_db_pdo->execute($sql_drop_table);

    //通过SQL生成数据库内容
    $sql_create_table = '
      CREATE TABLE [qtree] (
        [id] INTEGER  NOT NULL PRIMARY KEY,
        [title] VARCHAR(45)  NOT NULL,
        [pre] INTEGER  UNIQUE NOT NULL,
        [succ] INTEGER  UNIQUE NOT NULL,
        [parentid] INTEGER);';


    $this->kd_db_pdo->execute($sql_create_table);

    $sql_insert_data = array(
       " insert into [qtree] ([title],[pre],[succ],[parentid])  values('_virtual_root_',1,28,null);",       
       " insert into [qtree] ([title],[pre],[succ],[parentid])  values('s',2,27,1);",
       " insert into [qtree] ([title],[pre],[succ],[parentid])  values('KenDon',3,10,2);",
       " insert into [qtree] ([title],[pre],[succ],[parentid])  values('doc',4,5,3);",
       " insert into [qtree] ([title],[pre],[succ],[parentid])  values('test',6,7,3);",
       " insert into [qtree] ([title],[pre],[succ],[parentid])  values('lib',8,9,3);",
       " insert into [qtree] ([title],[pre],[succ],[parentid])  values('leo',11,18,2);",
       " insert into [qtree] ([title],[pre],[succ],[parentid])  values('kendon.leo',12,13,7);",
       " insert into [qtree] ([title],[pre],[succ],[parentid])  values('rencai.leo',14,15,7);",
       " insert into [qtree] ([title],[pre],[succ],[parentid])  values('myleo.leo',16,17,7);",
       " insert into [qtree] ([title],[pre],[succ],[parentid])  values('rencai5',19,26,2);",
       " insert into [qtree] ([title],[pre],[succ],[parentid])  values('app',20,21,11);",
       " insert into [qtree] ([title],[pre],[succ],[parentid])  values('core',22,23,11);",
       " insert into [qtree] ([title],[pre],[succ],[parentid])  values('docs',24,25,11);",
    );

    foreach($sql_insert_data as $item){
      $this->kd_db_pdo->execute($item);
    }

    
    $setting = array(
      'DBWrap'  =>  $this->DBWrap,
      'DBInfo'  =>  $this->DBInfo,
      'table'   =>  'qtree',
    );

    $this->fixture = new KD_QTree($setting);

    $this->testData = array(
        array( 'id'=>'1',  'pre'=>'1',  'succ'=>'28', parentid=>null, title=>'_virtual_root_',),
        array( 'id'=>'2',  'pre'=>'2',  'succ'=>'27', parentid=>'1',    title=>'s',),
        array( 'id'=>'3',  'pre'=>'3',  'succ'=>'10', parentid=>'2',    title=>'KenDon',),
        array( 'id'=>'4',  'pre'=>'4',  'succ'=>'5',  parentid=>'3',    title=>'doc',),
        array( 'id'=>'5',  'pre'=>'6',  'succ'=>'7',  parentid=>'3',    title=>'test',),
        array( 'id'=>'6',  'pre'=>'8',  'succ'=>'9',  parentid=>'3',    title=>'lib',),
        array( 'id'=>'7',  'pre'=>'11', 'succ'=>'18', parentid=>'2',    title=>'leo',),
        array( 'id'=>'8',  'pre'=>'12', 'succ'=>'13', parentid=>'7',    title=>'kendon',),
        array( 'id'=>'9',  'pre'=>'14', 'succ'=>'15', parentid=>'7',    title=>'recai.leo',),
        array( 'id'=>'10', 'pre'=>'16', 'succ'=>'17', parentid=>'7',    title=>'myleo.leo',),
        array( 'id'=>'11', 'pre'=>'19', 'succ'=>'26', parentid=>'2',    title=>'rencai5',),
        array( 'id'=>'12', 'pre'=>'20', 'succ'=>'21', parentid=>'11',   title=>'app',),
        array( 'id'=>'13', 'pre'=>'22', 'succ'=>'23', parentid=>'11',   title=>'core',),
        array( 'id'=>'14', 'pre'=>'24', 'succ'=>'25', parentid=>'11',   title=>'docs',),
      ); 
  }

  public function dearDown(){
  }
  
  public function testModelExists(){
    $this->assertEquals(true, is_a($this->kd_db_pdo, 'KD_Adapter_DB_PDO'));
  }

  public function testTreeExists(){
    $this->assertEquals(true, is_a( $this->fixture, 'KD_QTree') ); 
  }

  public function testGetRootNode(){

    $rootNode = $this->fixture->getRootNode();

    //检测SQL语句是否符合正确
    //$this->assertEquals( true, 'SELECT * FROM qtree WHERE succ = (SELECT MAX(succ) FROM qtree) ;' );
    
    $this->assertEquals(true, is_array($rootNode));

    $this->assertEquals(true, count($rootNode) >0 );

    $this->assertEquals(true, $rootNode['id'] === '1');
    $this->assertEquals(true, $rootNode['title'] === '_virtual_root_');
    $this->assertEquals(true, $rootNode['pre'] === '1');
    $this->assertEquals(true, $rootNode['succ'] === '28');
    $this->assertEquals(true, $rootNode['parentid'] === null);
    
    
    $this->assertEquals(true, $rootNode['id'] === $this->testData[0]['id']);
    $this->assertEquals(true, $rootNode['title'] === $this->testData[0]['title']);
    //$this->assertEquals(true, $rootNode === $this->testData[0]);
  }

  public function testIsNode(){
    
    $this->assertEquals(true, method_exists( $this->fixture, 'isNode' ) );

    $this->assertEquals(false, $this->fixture->isNode( array() ));

  }

}
